package com.itheima.controller;


import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    @PostMapping
    public Result save(@RequestBody Clazz clazz) {
        clazzService.save(clazz);
        return Result.success();
    }

    @GetMapping
    public Result page(ClazzQueryParam clazzQueryParam) {
        log.info("班级条件分页查询：{}",clazzQueryParam);
          PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
          return Result.success(pageResult);
    }

    @GetMapping("/{id:\\d+}")
    public Result getInfo(@PathVariable Integer id){
        log.info("根据id查询班级信息：{}",id);
        Clazz clazz =  clazzService.getInfo(id);
        return Result.success(clazz);
    }

    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息：{}",clazz);
         clazzService.update(clazz);
         return Result.success();
    }

    @DeleteMapping("/{id:\\d+}")
    public Result delete(@PathVariable Integer id){
        log.info("删除员工：{}",id);
        clazzService.delete(id);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        log.info("查询所有班级");
        List<Clazz>  clazzList= clazzService.findAll();
        return Result.success(clazzList);
    }
}
